package com.kaibes.orm.curd;

import java.util.ArrayList;
import java.util.List;

public class SelectBuilder {

    private boolean counted;
    private String tableName;
    private List<String> columns;
    private List<Query> queries;
    
    private List<String> orders;
    private List<OrderType> types;
    
    private int offset;
    private int size;

    public SelectBuilder() {
        columns = new ArrayList<>();
        queries = new ArrayList<>();
        orders = new ArrayList<>();
        types = new ArrayList<>();
    }
    
    public SelectBuilder setTableName(String tableName) {
        this.tableName = tableName;
        return this;
    }
    
    public SelectBuilder setCounted(boolean counted) {
        this.counted = counted;
        return this;
    }
    
    public SelectBuilder setQueries(List<Query> queries) {
        this.queries = queries;
        return this;
    }
    
    public SelectBuilder setColumns(List<String> columns) {
        this.columns = columns;
        return this;
    }
    
    public SelectBuilder setOffset(int offset) {
        this.offset = offset;
        return this;
    }
    
    public SelectBuilder setSize(int size) {
        this.size = size;
        return this;
    }
    
    public SelectBuilder addColumn(String column) {
        this.columns.add(column);
        return this;
    }
    
    public SelectBuilder addQuery(Query... queries) {
        for (Query query : queries) {
            if (query != null) {
                this.queries.add(query);
            }
        }
        return this;
    }
    
    public SelectBuilder addQuery(List<Query> queries) {
        this.queries.addAll(queries);
        return this;
    }
    
    public SelectBuilder addOrder(String orderBy, OrderType orderType) {
        orders.add(orderBy);
        types.add(orderType);
        return this;
    }
    
    public Select build() {
        Select select = new Select();
        select.setTableName(tableName);
        select.setColumns(columns);
        select.setQueries(queries);
        select.setOrders(orders);
        select.setTypes(types);
        select.setOffset(offset);
        select.setSize(size);
        select.setCounted(counted);
        return select;
    }
    
    public static SelectBuilder newBuilder() {
        return new SelectBuilder();
    }
}
